package com.lz.direct;

import com.lz.RabbitMQConnector;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.util.Scanner;

/**
 * 生产者
 */
public class ProducerDirect {
    public static void main(String[] args) throws Exception {
        String exchangeName = "lz_exchange";
        String queueName1 = "lz_queue_direct_1";
        String queueName2 = "lz_queue_direct_2";
        String queueName3 = "lz_queue_direct_3";
        String queueName4 = "lz_queue_direct_4";

        String routingKey1 = "key1";
        String routingKey2 = "key2";
        String routingKey3 = "key3";
        String routingKey4 = "key4";

        Channel channel = RabbitMQConnector.createConnectionAndChannel();

        /**
         * 创建交换机
         */
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, null);

        /**
         * 创建队列
         */
        channel.queueDeclare(queueName1, true, false, false, null);
        channel.queueDeclare(queueName2, true, false, false, null);
        channel.queueDeclare(queueName3, true, false, false, null);
        channel.queueDeclare(queueName4, true, false, false, null);

        /**
         * 绑定队列和交换机
         * 1.队列名称
         * 2.交换机名称
         * 3.路由键，在直连模式下，可以为我们的队列名称
         */
        channel.queueBind(queueName1, exchangeName, routingKey1);
        channel.queueBind(queueName2, exchangeName, routingKey1);
        channel.queueBind(queueName3, exchangeName, routingKey3);
        channel.queueBind(queueName4, exchangeName, routingKey4);


        /**
         * 发送消息
         * 1.发送到哪个交换机
         * 2.路由键 routingKey
         * 3.其他参数信息
         * 4.消息主体
         */
        channel.basicPublish(exchangeName, routingKey1, null, "key1 message".getBytes());
        channel.basicPublish(exchangeName, routingKey2, null, "key2 message".getBytes());
        channel.basicPublish(exchangeName, routingKey3, null, "key3 message".getBytes());
        channel.basicPublish(exchangeName, routingKey4, null, "key4 message".getBytes());
        RabbitMQConnector.closeResources();
    }
}
